package com.fr.web.core.service;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.cache.DiskCacheMap;
import com.fr.base.core.UUID;
import com.fr.base.file.ClusterConfigManager;
import com.fr.report.web.ui.IconManager;
import com.fr.report.web.ui.WidgetManager;
import com.fr.util.Base64;
import com.fr.web.Browser;
import com.fr.web.cluster.ClusterService;
import com.fr.web.core.Attachment;
import com.fr.web.core.WebUtils;
import com.fr.web.core.upload.SmartFile;
import com.fr.web.core.upload.SmartFiles;
import com.fr.web.core.upload.SmartUpload;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AttachmentService extends NoSessionIDService
{
  private static AttachmentService ATTACHMENT_SERVICE = null;
  private static Map attachmentMap = new DiskCacheMap();
  private static final String UPLOAD = "attach_upload";
  private static final String DOWNLOAD = "attach_download";
  private static final String IMAGE = "attach_image";
  private static final String RELEASE = "attach_release";

  public static AttachmentService getInstance()
  {
    if (ATTACHMENT_SERVICE == null)
      ATTACHMENT_SERVICE = new AttachmentService();
    return ATTACHMENT_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("attach_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if ("attach_upload".equals(paramString))
      dealWithUploadAttachment(paramHttpServletRequest, paramHttpServletResponse);
    else if ("attach_download".equals(paramString))
      dealWithDownloadAttachment(paramHttpServletRequest, paramHttpServletResponse);
    else if ("attach_image".equals(paramString))
      dealWithImageAttachment(paramHttpServletRequest, paramHttpServletResponse);
    else if ("attach_release".equals(paramString))
      dealWithReleaseAttachment(paramHttpServletRequest);
  }

  public static Attachment addAttachment(Image paramImage)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    BaseUtils.writeImage(paramImage, "png", localByteArrayOutputStream);
    return addAttachment("image", "attach.png", localByteArrayOutputStream.toByteArray());
  }

  public static Attachment addAttachment(String paramString1, String paramString2, byte[] paramArrayOfByte)
  {
    String str = UUID.randomUUID().toString();
    if (FRContext.getClusterConfigManager().isUseCluster())
    {
      localObject = FRContext.getClusterConfigManager().getSelfService();
      if ((localObject != null) && (((ClusterService)localObject).getServiceName() != null))
        str = ((ClusterService)localObject).getServiceName() + "_" + str;
    }
    Object localObject = new Attachment(str, paramString1, paramString2, paramArrayOfByte);
    attachmentMap.put(str, localObject);
    return ((Attachment)localObject);
  }

  public static Attachment getAttachment(String paramString)
  {
    return ((Attachment)attachmentMap.get(paramString));
  }

  public static void removeAttachment(String paramString)
  {
    attachmentMap.remove(paramString);
  }

  private static void dealWithUploadAttachment(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletContext localServletContext = paramHttpServletRequest.getSession().getServletContext();
    SmartUpload localSmartUpload = new SmartUpload();
    localSmartUpload.initialize(localServletContext, paramHttpServletRequest, paramHttpServletResponse);
    localSmartUpload.upload();
    SmartFiles localSmartFiles = localSmartUpload.getFiles();
    if (localSmartFiles.getSize() == 0L)
      return;
    SmartFile localSmartFile = localSmartFiles.getFile(0);
    Attachment localAttachment = addAttachment((localSmartFile.getContentType().indexOf("image") > 0) ? "image" : "other", localSmartFile.getFileName(), localSmartFile.getBytes());
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(localAttachment.toConfig());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithDownloadAttachment(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
    if (str1 != null)
    {
      Attachment localAttachment = getAttachment(str1);
      if (localAttachment != null)
      {
        byte[] arrayOfByte = localAttachment.getBytes();
        paramHttpServletResponse.setHeader("Pragma", "No-cache");
        paramHttpServletResponse.setHeader("Cache-Control", "max-age=180");
        String str2 = localAttachment.getFilename();
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + Browser.resolve(paramHttpServletRequest).getEncodedFileName4Download(str2));
        str2 = str2.toLowerCase();
        if (str2.endsWith(".pdf"))
        {
          paramHttpServletResponse.setContentType("application/pdf");
          paramHttpServletResponse.setHeader("extension", "pdf");
        }
        else if (str2.endsWith(".xls"))
        {
          paramHttpServletResponse.setContentType("application/x-excel");
          paramHttpServletResponse.setHeader("extension", "xls");
        }
        else if (str2.endsWith(".doc"))
        {
          paramHttpServletResponse.setContentType("application/msword");
          paramHttpServletResponse.setHeader("extension", "doc");
        }
        else if (str2.endsWith(".svg"))
        {
          paramHttpServletResponse.setContentType("image/svg+xml");
          paramHttpServletResponse.setHeader("extension", "svg");
        }
        else if (str2.endsWith(".csv"))
        {
          paramHttpServletResponse.setContentType("application/octet-stream");
          paramHttpServletResponse.setHeader("extension", "csv");
        }
        else if (str2.endsWith(".txt"))
        {
          paramHttpServletResponse.setContentType("application/octet-stream");
          paramHttpServletResponse.setHeader("extension", "txt");
        }
        else
        {
          paramHttpServletResponse.setContentType("application/x-msdownload");
        }
        ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
        localServletOutputStream.write(arrayOfByte);
        localServletOutputStream.flush();
        localServletOutputStream.close();
      }
    }
  }

  private static void dealWithImageAttachment(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=86400");
    paramHttpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + 86400000L);
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
    if (str != null)
    {
      byte[] arrayOfByte = null;
      if (str.startsWith("icon_name_"))
      {
        arrayOfByte = FRContext.getWidgetManager().getIconManager().getIconImage4Web(WebUtils.createServletURL(paramHttpServletRequest), str);
      }
      else if (str.equals("toolbar-image.png"))
      {
        arrayOfByte = FRContext.getWidgetManager().getIconManager().getAllInOneImageBytes(WebUtils.createServletURL(paramHttpServletRequest));
      }
      else
      {
        localObject = getAttachment(str);
        if (localObject != null)
          arrayOfByte = ((Attachment)localObject).getBytes();
      }
      if (arrayOfByte == null)
        arrayOfByte = new byte[0];
      Object localObject = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "format");
      if ("base64".equals(localObject))
        arrayOfByte = Base64.encode(arrayOfByte).getBytes("ISO-8859-1");
      else
        paramHttpServletResponse.setContentType("image/png");
      ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
      try
      {
        localServletOutputStream.write(arrayOfByte);
      }
      catch (Exception localException1)
      {
      }
      try
      {
        localServletOutputStream.flush();
        localServletOutputStream.close();
      }
      catch (Exception localException2)
      {
      }
    }
  }

  private static void dealWithReleaseAttachment(HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
    if (str != null)
      removeAttachment(str);
  }

  private static class NullImage extends Image
  {
    private static NullImage nullImage = null;
    private static byte[] nullBytes = null;

    public static synchronized NullImage getNullImageInstance()
    {
      if (nullImage == null)
        nullImage = new NullImage();
      return nullImage;
    }

    public static synchronized byte[] getNullImageBytes()
    {
      if (nullBytes == null)
      {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        BaseUtils.writeImage(getNullImageInstance(), "png", localByteArrayOutputStream);
        try
        {
          localByteArrayOutputStream.flush();
          localByteArrayOutputStream.close();
        }
        catch (IOException localIOException)
        {
          FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
        }
        nullBytes = localByteArrayOutputStream.toByteArray();
      }
      return nullBytes;
    }

    public void flush()
    {
    }

    public Graphics getGraphics()
    {
      return null;
    }

    public int getHeight(ImageObserver paramImageObserver)
    {
      return 0;
    }

    public Object getProperty(String paramString, ImageObserver paramImageObserver)
    {
      return null;
    }

    public ImageProducer getSource()
    {
      return null;
    }

    public int getWidth(ImageObserver paramImageObserver)
    {
      return 0;
    }
  }
}